package com.jf.net.gson

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter

/**
 * StringNullAdapter
 * @author JackyFlame
 * @time 2023/10/20 14:16
 * @description
 */
class StringNullAdapter : TypeAdapter<String>() {

    override fun write(out: JsonWriter, value: String?) {
        out.value(value ?: "")
    }

    override fun read(jsonReader: JsonReader): String {
        if (jsonReader.peek() == JsonToken.NULL) {
            jsonReader.nextNull()
            return ""
        }
        return jsonReader.nextString()
    }
}